package com.huya.k12.translate.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.tmatesoft.svn.core.SVNCommitInfo;
import org.tmatesoft.svn.core.SVNDepth;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNLogEntry;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;
import org.tmatesoft.svn.core.internal.wc.admin.SVNLog;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.ISVNOptions;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNCommitClient;
import org.tmatesoft.svn.core.wc.SVNLogClient;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNStatus;
import org.tmatesoft.svn.core.wc.SVNStatusClient;
import org.tmatesoft.svn.core.wc.SVNUpdateClient;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

@Component
public class SVNUtil {

	private Logger logger = Logger.getLogger(SVNUtil.class);
	private SVNClientManager clientManager;
	private File workingCopyFile;

	@Value("${svn.userName}")
	private String userName;

	@Value("${svn.password}")
	private String password;

	@Value("${svn.url}")
	private String svnURL;

	@Value("${svn.workingCopyPath}")
	private String workingCopyPath;
	
	@Value("${xml.path}")
	private String path;

	private void init() {
		SVNRepositoryFactoryImpl.setup();
		DAVRepositoryFactory.setup();
		SVNURL repositoryURL = null;
		try {
			repositoryURL = SVNURL.parseURIEncoded(svnURL);
		} catch (SVNException e) {
			// TODO Auto-generated catch block
			logger.error(e.getErrorMessage(), e);
		}
		ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
		clientManager = SVNClientManager.newInstance((DefaultSVNOptions) options, userName, password);
	}

	public long update() {
		SVNUpdateClient updateClient = getClientManager().getUpdateClient();
		long versionNum = 0;
		try {
			versionNum = updateClient.doUpdate(getWorkingCopyFile(), SVNRevision.HEAD, SVNDepth.INFINITY, false, false);
			logger.info("current svn version is:" + versionNum);
		} catch (SVNException e) {
			// TODO Auto-generated catch block
			logger.error(e.getErrorMessage(), e);
		}
		return versionNum;
	}

	public SVNCommitInfo commit() {
		SVNCommitClient commitClient = getClientManager().getCommitClient();
		SVNCommitInfo commitInfo = null;
		try {
			commitInfo = commitClient.doCommit(new File[] { getWorkingCopyFile() }, true, "", null, null, false, false,
					SVNDepth.INFINITY);
		} catch (SVNException e) {
			// TODO Auto-generated catch block
			logger.error(e.getErrorMessage(), e);
		}
		return commitInfo;
	}
	
	
    public void addEntry(File wcPath) {
        try {  
            getClientManager().getWCClient().doAdd(new File[] { wcPath }, false,
                    false, false, SVNDepth.fromRecurse(true), false, false,
                    true);  
        } catch (SVNException e) {  
            logger.error(e.getErrorMessage(), e);  
        }  
    }


	public SVNClientManager getClientManager() {
		if (clientManager != null)
			return clientManager;
		init();
		return clientManager;
	}

	public void setClientManager(SVNClientManager clientManager) {
		this.clientManager = clientManager;
	}

	public File getWorkingCopyFile() {
		workingCopyFile = new File(workingCopyPath);
		return workingCopyFile;
	}

	public void setWorkingCopyFile(File workingCopyFile) {
		this.workingCopyFile = workingCopyFile;
	}

}
